package com.bravo.search.controller;

import com.bravo.common.entity.PageResult;
import com.bravo.common.entity.Result;
import com.bravo.common.entity.StatusCode;
import com.bravo.search.entity.Article;
import com.bravo.search.service.ArticleSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author: Yuyong
 * @create: 2019-08-12 10:51
 **/

@RestController
@CrossOrigin
@RequestMapping("/articleSearch")
public class ArticleSearchController {

    @Autowired
    private ArticleSearchService articleSearchService;

    @PostMapping
    public Result save(@RequestBody Article article){
        articleSearchService.add(article);
         return new Result(true, StatusCode.OK,"操作成功");
    }

    @GetMapping(value = "/search/{page}/{size}/{keywords}")
    public Result findByTitleLike(@PathVariable String keywords,@PathVariable int page,@PathVariable int size) throws UnsupportedEncodingException {
        keywords = URLDecoder.decode(keywords, "UTF-8");
        Page<Article> articlePage = articleSearchService.findByTitleLike(keywords, page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult<Article>(articlePage.getTotalElements(), articlePage.getContent()));
    }


}
